package com.jiexinyun.security;

import com.jiexinyun.dto.response.AjaxResult;
import com.jiexinyun.utils.ServletUtils;
import com.jiexinyun.error.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * 认证失败处理类 返回未授权
 * 
 * @author ruoyi
 */
@Component
@Slf4j
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e)
            throws IOException {
        //请求访问：%s，认证失败，无法访问系统资源，错误码：%d
        AjaxResult ajaxResult = AjaxResult.error(ErrorCode.UNAUTHORIZED);
        ServletUtils.renderString(response, ajaxResult);
    }
}
